<?php
namespace app\services;

use app\utils\Functions as F;

class MediaListService extends BaseService {
    public $matrix = [
        'id' => 'id',
        'name' => 'name',
        'belongChannel' => ['ad_network', '$v=="vamaker" && $v=\Yii::t("common", "ValueMaker");
                                           $v=="valuemaker"&& $v=\Yii::t("common", "ValueMaker");
                                           return $v;'],
        'trafficType'   => 'traffic_type',
        'size' => 'size',
        'allowFormat' => ['format_list', '$dpic = \Yii::t("common", "dynamic-pic");
                                          $spic = \Yii::t("common", "static-pic");
                                          $v=str_replace("1", $spic, $v); 
                                          $v=str_replace("2", $dpic, $v);
                                          $v=str_replace("3", "SWF", $v); 
                                          $v=str_replace("4", "TXT", $v);
                                          $v=str_replace("5", "flv", $v);
                                          $v=str_replace("6", "mp4", $v);
                                          $v=str_replace("7", "OGG", $v);
                                          $v=str_replace("8", "WebM", $v);
                                          return $v;'],
        'systemType'   => 'systemType',
        'position' => 'show_form',  
        'price' => ['base_price', 'return $v;'],
        // 'blackList' => ['id', 'return "";'],
        'type' => 'category_name',
    ];

    public function getAllAdspaces () {
        $adspace = $this->getAdspaces('Adspace');
        $vadspace = $this->getAdspaces('VideoAdspace');
        $madspace = $this->getAdspaces('MobileAdspace');
        $mvadspace = $this->getAdspaces('MobileVideoAdspace');
        $adspace = array_merge($adspace, $vadspace, $madspace, $mvadspace);
        return $adspace;
    }

    public function getAdspaces($table) {
        $traffics = $this->S('traffic_slot');
        if ( $table == 'Adspace' && !in_array('1', $traffics)) {
            return [];
        } else if ($table == 'VideoAdspace' && !in_array('2', $traffics)) {
            return [];
        } else if ($table == 'MobileAdspace' && !in_array('3', $traffics)) {
            return [];
        } else if ($table == 'MobileVideoAdspace' && !in_array('4', $traffics)) {
            return [];
        }

        $adnetworks = $this->S('adnetwork_slot');
        $medias = $this->S('media_slot');

        $adnetworkWhere = ' id in ('.implode(',', $adnetworks).')';
        $adspaceWhere = 'ad_network_id in ('.implode(',', $adnetworks).')';
        if (in_array('0', $adnetworks)) {
            $adnetworkWhere = '';
            $adspaceWhere = '';
        }

        if (in_array('0', $medias)) {
            $adspaceWhere .= '';
        } else {
            $mediaWhere = ' id in ('.implode(',', $medias).')';
            $sql = "select media_id from {{%adnetwork_media}} where".$mediaWhere;
            $medias = $this->query($sql);
            $medias = array_column($medias, 'media_id');
            $adspaceWhere && $adspaceWhere .= ' and media_id in ('.implode(',', $medias).')';
            !$adspaceWhere && $adspaceWhere .= ' media_id in ('.implode(',', $medias).')';
        }
        $adspaceWhere && $adspaceWhere .= ' and status = 0 ';
        !$adspaceWhere && $adspaceWhere .= ' status = 0 ';
        $ad_network = $this->getAll('AdNetwork', $adnetworkWhere);
        $adspaces = $this->getAll($table, $adspaceWhere);
        $categorys = $this->getCategorys();
        $adspaces = array_values($adspaces);
        foreach ($adspaces as &$adspace) {
            if ($this->S('user_rank') == 'advertiser') {
                $adspace['base_price'] = '';
            }
            $adspace['ad_network'] = $ad_network[$adspace['ad_network_id']]['name']; 
            $adspace['size'] = $adspace['width'].'*'.$adspace['height'];
            $table == 'MobileAdspace' && $adspace['size'] = $adspace['ext_size'];
            list($adspace['systemType'], $adspace['show_form'], $adspace['traffic_type']) = $this->getAdspaceSystemType($adspace, $table);
            $adspace['category_name'] = '--';
            if (isset($adspace['category_id'])) {
                $key = $adspace['ad_network_id'].'$$'.$adspace['category_id'];
                $adspace['ad_network_id'] == 2 && $key ='1'.'$$'.$adspace['category_id'];
                if (isset($categorys[$key])) {
                    $adspace['category_name'] = $categorys[$key]['name'];
                }
            }

            if (isset($adspace['type'])) {
                if ($adspace['type'] != '1' && isset($adspace['app_category'])) {
                    $type = (int)$adspace['type']-1;
                    $key = $adspace['ad_network_id'].'$$'.$type.'$$'.$adspace['app_category'];
                    $adspace['ad_network_id'] == 2 && $key ='1'.'$$'.$type.'$$'.$adspace['app_category'];
                    if (isset($categorys[$key])) {
                        if (!$categorys[$key]['cate_name'] || $categorys[$key]['cate_name'] == $categorys[$key]['name']) {
                            $adspace['category_name'] = $categorys[$key]['name'];
                        } else {
                            $adspace['category_name'] = $categorys[$key]['cate_name'].'-'. $categorys[$key]['name'];
                        }  
                    }
                } 
            }
        }
        return $this->fieldsMap($this->matrix, $adspaces); 
    }

    public function getCategorys() {
        $sql = "select ad_network_id, category_id, name from {{%adspace_category}}";
        $categorys = $this->query($sql); $newCategory = [];
        foreach ($categorys as $category) {
            $key = $category['ad_network_id'].'$$'.$category['category_id'];
            $newCategory[$key] = $category;
        }

        $sql = "select s.ad_network_id, s.market, s.market_id, s.name, f.name cate_name 
            from {{%mobile_app_category}} as s 
            left join {{%mobile_app_category}} as f 
            on s.pid = f.category_id and s.ad_network_id = f.ad_network_id";
        $categorys = $this->query($sql);
        foreach ($categorys as $category) {
            $key = $category['ad_network_id'].'$$'.$category['market'].'$$'.$category['market_id'];
            $newCategory[$key] = $category;
        }
        return $newCategory;
    }

    public function getAdspaceSystemType($adspace, $table) {
        $type = '';
        $form = '';
        $traffic = '';
        switch ($table)
        {
            case 'Adspace':
                $type = 'PC';
                $traffic = 'PC-Display';
                if ($adspace['position'] == 1) $type .= '-'.$this->t('common', 'First Screen');
                else if ($adspace['position'] == 2) $type .= '-'.$this->t('common', 'Second Screen');
                else if ($adspace['position'] == 3) $type .= '-'.$this->t('common', 'Third Screen');
                else $type .= '-'.$this->t('common', 'Unknown');

                $adspace['ad_form'] == 1 && $form = $this->t("common", "PC Banner");
                $adspace['ad_form'] == 2 && $form = $this->t("common", "PC Popup");
                $adspace['ad_form'] == 3 && $form = $this->t("common", "Client");
                $adspace['ad_form'] == 4 && $form = $this->t("common", "Video Pause");
                $adspace['ad_form'] == 12 && $form = $this->t("common", "Popup");
                break;
            case 'VideoAdspace':
                $type = 'PC';
                $traffic = 'PC-Video';
                $adspace['ad_form'] == 0 && $form = $this->t("common", "Pre Roll");
                $adspace['ad_form'] == 1 && $form = $this->t("common", "Mid Roll");
                $adspace['ad_form'] == 2 && $form = $this->t("common", "Post Roll");
                $adspace['ad_form'] == 3 && $form = $this->t("common", "Corner Mark");
                $adspace['ad_form'] == 4 && $form = $this->t("common", "Overlay");
                break;
            case 'MobileAdspace':
                if ($adspace['type'] == 1) {
                    $type = 'Web';
                    $traffic = 'Mobile-Web';
                    if ($adspace['position'] == 1) $type .= '-'.$this->t('common', 'First Screen');
                    else if ($adspace['position'] == 2) $type .= '-'.$this->t('common', 'Second Screen');
                    else if ($adspace['position'] == 3) $type .= '-'.$this->t('common', 'Third Screen');
                    else $type .= '-'.$this->t('common', 'Unknown');
                } else if ($adspace['type'] == 2) {
                    $type = 'Android';
                    $traffic = 'Mobile-Android-Display';
                } else if ($adspace['type'] == 3) {
                    $type = 'iOS';
                    $traffic = 'Mobile-iOS-Display';
                }
                if ($adspace['size_form']==1) $form = $this->t("common", "Web Banner");
                if ($adspace['size_form']==2) $form = $this->t("common", "App Banner");
                if ($adspace['size_form']==3) $form = $this->t("common", "App Splash");
                if ($adspace['size_form']==4) $form = $this->t("common", "Interstitial");
                if ($adspace['size_form']==5) $form = $this->t("common", "Feeds");
                break;
            case 'MobileVideoAdspace':
                if ($adspace['platform'] == 2) {
                    $type = 'Android';
                    $traffic = 'Mobile-Android-Video';
                } else if ($adspace['platform'] == 3) {
                    $type = 'iOS';
                    $traffic = 'Mobile-iOS-Video';
                }
                $adspace['ad_form'] == 0 && $form = $this->t("common", "Pre Roll");
                $adspace['ad_form'] == 1 && $form = $this->t("common", "Mid Roll");
                $adspace['ad_form'] == 2 && $form = $this->t("common", "Post Roll");
                $adspace['ad_form'] == 3 && $form = $this->t("common", "Corner Mark");
                $adspace['ad_form'] == 4 && $form = $this->t("common", "Overlay");
                break;
            default:
                break;
        }
        return [$type, $form, $traffic];
    }

    public function getAllAdNetWork() {
        $adnetworks = $this->S('adnetwork_slot');
        $adnetworkWhere = ' id in ('.implode(',', $adnetworks).')';
        if (in_array('0', $adnetworks)) {
            $adnetworkWhere = '';
        }
        $datas = $this->getAll('AdNetwork', $adnetworkWhere);
        $matrix = [
            'id'        => 'id',
            'name'   => ['name', '$v=="vamaker"&&$v=\Yii::t("resource", "ValueMaker");
                                     return $v;'],
        ];
        $datas = $this->fieldsMap($matrix, $datas);
        return $datas;
    }

}//end
